from django.shortcuts import render,redirect,HttpResponse
from zhaopin_table import models
from django.db.models import Min,Avg,Max,Sum,Count

list=['上海','西安','北京','深圳','广州','天津',
      '大连','杭州','西安','南京','成都','菲律宾',
      '武汉','苏州','厦门','重庆','青岛','沈阳',
      '成都']


def android(request):
    myusername=request.user.username

    android_list=[]
    id=0

    for i in list:
        id+=1
        res = models.Job.objects.filter(region__icontains=i, tid__in='5')
        price_min = res.aggregate(price=Min('pay'))
        price_max = res.aggregate(price=Max('pay'))
        price_avg=res.aggregate(price=Avg('pay'))
        count=res.aggregate(count=Count('id'))

        dict = {'id':id,'name':i,'price_max':price_max['price'],
                'price_min':price_min['price'], 'price': round(price_avg['price'],2),
                'count': count['count']}

        android_list.append(dict)

    return render(request,'tables_android.html',{"android_list":android_list,'username':myusername})


def java(request):
    myusername=request.user.username

    java_list = []
    id = 0

    for i in list:
        id += 1
        res = models.Job.objects.filter(region__icontains=i, tid__in='1')
        price_min = res.aggregate(price=Min('pay'))
        price_max = res.aggregate(price=Max('pay'))
        price_avg = res.aggregate(price=Avg('pay'))
        count = res.aggregate(count=Count('id'))

        dict = {'id': id, 'name': i, 'price_max': price_max['price'],
                'price_min': price_min['price'], 'price': round(price_avg['price'], 2),
                'count': count['count']}

        java_list.append(dict)
    return render(request,'tables_java.html',{"java_list":java_list,'username':myusername})


def php(request):
    myusername=request.user.username

    php_list = []
    id = 0

    for i in list:
        id += 1
        res = models.Job.objects.filter(region__icontains=i, tid__in='3')
        price_min = res.aggregate(price=Min('pay'))
        price_max = res.aggregate(price=Max('pay'))
        price_avg = res.aggregate(price=Avg('pay'))
        count = res.aggregate(count=Count('id'))

        dict = {'id': id, 'name': i, 'price_max': price_max['price'],
                'price_min': price_min['price'], 'price': round(price_avg['price'], 2),
                'count': count['count']}

        php_list.append(dict)
    return render(request,'tables_php.html',{"php_list":php_list,'username':myusername})


def python(request):
    myusername=request.user.username

    python_list = []
    id = 0

    for i in list:
        id += 1
        res = models.Job.objects.filter(region__icontains=i, tid__in='4')
        price_min = res.aggregate(price=Min('pay'))
        price_max = res.aggregate(price=Max('pay'))
        price_avg = res.aggregate(price=Avg('pay'))
        count = res.aggregate(count=Count('id'))
        # print(count)
        dict = {'id': id, 'name': i, 'price_max': price_max['price'],
                'price_min': price_min['price'], 'price': round(price_avg['price'], 2),
                'count': count['count']}

        python_list.append(dict)
    return render(request,'tables_python.html',{"python_list":python_list,'username':myusername})

def web(request):
    myusername=request.user.username

    web_list = []
    id = 0

    for i in list:
        id += 1
        res = models.Job.objects.filter(region__icontains=i, tid__in='2')
        price_min = res.aggregate(price=Min('pay'))
        price_max = res.aggregate(price=Max('pay'))
        price_avg = res.aggregate(price=Avg('pay'))
        count = res.aggregate(count=Count('id'))

        dict = {'id': id, 'name': i, 'price_max': price_max['price'],
                'price_min': price_min['price'], 'price': round(price_avg['price'], 2),
                'count': count['count']}

        web_list.append(dict)
    return render(request,'tables_web.html',{"web_list":web_list,'username':myusername})